const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  role: {
    type: String,
    enum: ['user', 'teacher'],
    default: 'user'
  },
  studentId: {
    type: String,
    required: function() {
      return this.role === 'user';
    }
  }
}, { timestamps: true });

// 密码加密中间件
// userSchema.pre('save', async function(next) {
  // if (!this.isModified('password')) return next();
  // this.password = await bcrypt.hash(this.password, 10);
  // next();
// });

// 验证密码方法
userSchema.methods.comparePassword = async function(candidatePassword) {
  return await  this.password===candidatePassword;
};

module.exports = mongoose.model('User', userSchema); 